package gui;
import java.awt.Color;

import javax.swing.JTextPane;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.rtf.RTFEditorKit;


public class ColoredTextPane extends JTextPane{
	private static final long serialVersionUID = 1L;
	SimpleAttributeSet style;
	Document doc;

	public ColoredTextPane() {
		super();
		style = new SimpleAttributeSet();
		StyleConstants.setFontFamily(style, "monospaced");
		setContentType("text/rtf" );
		
		setEditorKit(new RTFEditorKit());
		setEditable(false);
		doc = this.getDocument();
	}

	private synchronized void append(String msg, Color color) {
		StyleConstants.setForeground(style, color);
		int len = doc.getLength();
		try {
			doc.insertString(len, msg, style);
		} catch (BadLocationException e1) {
			System.out.print("Failed to append msg ["+msg+"]" );
			e1.printStackTrace();
		}
	}

	public void println(String msg, Color color) {
		append(msg+"\r\n", color);
	}
	
}
